<template>
  <div class="com-contatiner">
    <div class="com-chart" ref="map_ref" @dblclick="reaverMap"></div>
  </div>
</template>

<script>
import axios from 'axios'
import { mapState } from 'vuex'
import { getProvinceMapInfo } from '@/utils/map_utils.js'
export default {
  data () {
    return {
      chertInstane: null,
      allData: '',
      dataMap: []
    }
  },
  created () {
    this.$socket.registerCallBack('mapData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: ''
    })
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('mapData')
  },
  methods: {
    async initChart () {
      this.chertInstane = this.$echarts.init(this.$refs.map_ref, this.theme)
      const { data: res } = await axios.get('http://localhost:8080/map/china.json')
      this.$echarts.registerMap('china', res)
      // 初始化echarts的数据
      const initOption = {
        title: {
          text: '▍ 商家分布',
          top: 20,
          left: 20
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2E72EF',
            bordeColor: '#333'
          },
          roam: true
        }
      }
      // 展示
      this.chertInstane.setOption(initOption)
      // 地图的点击事件
      this.chertInstane.on('click', async val => {
        const w = getProvinceMapInfo(val.name)
        if (w.key) {
          const { data: res } = await axios.get('http://localhost:8080' + w.path)
          this.$echarts.registerMap(w.key, res)
          const MapOption = {
            geo: {
              map: w.key
            }
          }
          this.chertInstane.setOption(MapOption)
        }
      })
    },
    // 获取服务器数据
    getData (res) {
      // const { data: res } = await this.$http.get('map')
      this.allData = res
      // 调用updataChart方法
      this.updataChart()
    },
    // 处理数据
    updataChart () {
      const legendArr = this.allData.map(val => {
        return val.name
      })
      const serierArr = this.allData.map(val => {
        return {
          type: 'effectScatter',
          rippleEffect: {
            scale: 10,
            brushType: 'stroke'
          },
          name: val.name,
          data: val.children,
          coordinateSystem: 'geo'
        }
      })
      console.log(serierArr)
      const dataOption = {
        legend: {
          data: legendArr
        },
        series: serierArr
      }
      this.chertInstane.setOption(dataOption)
    },
    // 屏幕自适应
    screenAdapter () {
      const titleSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleSize
          }
        },
        legend: {
          itemWidth: titleSize / 2,
          itemHeight: titleSize / 2,
          itemGap: titleSize / 2,
          textStyle: {
            fontSize: titleSize / 2.5
          }
        }
      }
      this.chertInstane.setOption(adapterOption)
      this.chertInstane.resize()
    },
    reaverMap () {
      const reaverOption = {
        geo: {
          map: 'china'
        }
      }
      this.chertInstane.setOption(reaverOption)
    }
  },
  computed: {
    // eslint-disable-next-line no-undef
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chertInstane.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updataChart() // 更新图表的展示
    }
  }
}
</script>

<style>
</style>
